<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/9 0009
 * Time: 21:50
 */

namespace App\Server;


use App\Model\Classify;
Use Blade;
//use Illuminate\Support\Facades\Blade;

class TagServer
{
public function make(){
$this->classify();
$this->next();
$this->forward();

}
//定义classify标签
public function classify(){
    Blade::directive('classify',function ($parameter){
        $parameter=  $parameter?$parameter:'[]';
        $php=<<<php
<?php
        \$param=$parameter;
        \$db=new \App\Model\Classify();
        
       if (isset(\$param['name'])){
        \$db=\$db->where('name',\$param['name']);
        };
        if (isset(\$param['pid'])){
        \$db=\$db->where('pid',\$param['pid']);
        };
         if (isset(\$param['id'])){
        \$db=\$db->where('id',\$param['id']);
        };
        \$classifys=\$db->get();
        foreach (\$classifys as \$k=>\$classify){
?>
php;
return $php;
    });
    Blade::directive('endclassify',function (){
       $php=<<<php
<?php
};
?>
php;
return $php;
    });
}
//定义上一篇标签
public function forward(){
    //定义单标签
Blade::directive('forward',function (){
        $php=<<<php
  <?php
                            \$id=request()->route('news')->id;
                           \$news=\App\Model\News::where('id','<',\$id)->orderBy('id','DESC')->first();
                           if (\$news){
                               echo '<a href="'.route('news.news.show',\$news['id']).'">'.\$news['title'].'</a>';
                           }else{
                               echo '';
                           }
                      ?>
php;
        return $php;
    });
}
//定义下一篇标签
public function next(){
    Blade::directive('next',function (){
    $php=<<<php
  <?php
                            \$id=request()->route('news')->id;
                           \$news=\App\Model\News::where('id','>',\$id)->orderBy('id','ASC')->first();
                           if (\$news){
                               echo '<a href="'.route('news.news.show',\$news['id']).'">'.\$news['title'].'</a>';
                           }else{
                               echo '';
                           }
                      ?>
php;
return $php;
    });
}
}